//package com.site.springboot.core.filter;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.site.springboot.core.service.impl.JwtService;
//import jakarta.servlet.FilterChain;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import java.io.IOException;
//
///**
// * Project:springboot-project-news-publish-system
// * ClassName:JwtAuthenticationTokenFilter
// * Package:com.site.springboot.core.filter
// * User:CWQ
// * Description:
// *
// * @Author 合肥-陈万琼
// * @Create 2024/5/28 0:24
// * @Version 1.0
// */
//@Component
//public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
//    @Autowired
//    private JwtService jwtService;
//    @Autowired
//    private UserDetailsService userDetailsService;
//    @Override
//    protected void doFilterInternal(HttpServletRequest request,
//                                    HttpServletResponse response,
//                                    FilterChain filterChain) throws ServletException, IOException {
//        System.out.println("JwtAuthenticationTokenFilter" + request.getRequestURI());
//        String authorization = request.getHeader("Authorization");
//        String token = null;
//        String username = null;
//        if (StringUtils.hasLength(authorization)) {
//            token = authorization.replace("Bearer ", "");
////            token = authorization.substring(7);
//            username = jwtService.extractUsername(token);
//        }
//        if (username != null) {
//            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
//            if (jwtService.validateToken(token, userDetails)) {
//                UsernamePasswordAuthenticationToken authToken =
//                        new UsernamePasswordAuthenticationToken(userDetails,null, userDetails.getAuthorities());
//                authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//                SecurityContextHolder.getContext().setAuthentication(authToken);
//            }
//        }
//        filterChain.doFilter(request, response);
//    }
//}
